﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Husb.Util;
using Husb.Util.Enum;
using CentralOffice.Archive.Data;
using CentralOffice.Archive.BusinessActions;

using CategoryTable = CentralOffice.Archive.Data.CategoryDataSet.CategoryDataTable;
using CategoryRow = CentralOffice.Archive.Data.CategoryDataSet.CategoryRow;

using ArchiveTable = CentralOffice.Archive.Data.ArchiveDataSet.ArchivesDataTable;
using ArchiveRow = CentralOffice.Archive.Data.ArchiveDataSet.ArchivesRow;
using System.Data;

namespace CentralOffice.WebClient.Archives
{
    public partial class ArchiveAuthority : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string id = HttpContext.Current.Request.QueryString["id"];
                ArchiveRow archive = CentralOffice.Archive.BusinessActions.Archive.GetById(new Guid(id));
                if (archive != null)
                {
                    #region
                    lblCustomer.Text = archive.Customer;
                    //lblQueryNumber.Text = archive.QueryNumber;
                    lblAddress.Text = archive.Address;
                    lblTelephone1.Text = archive.Telephone1;
                    //lblTelephone2.Text = archive.Telephone2;
                    //lblFax.Text = archive.Fax;
                    //lblEmail.Text = archive.Email;
                    lblContact.Text = archive.Contact;
                    //lblContactTitle.Text = archive.ContactTitle;
                    //lblContactPhone.Text = archive.ContactPhone;


                    lblName.Text = archive.Name;
                    //lblQueryNumber.Text = archive.QueryNumber;
                    lblSecretLevel.Text = archive.SecretLevel;
                    lblProvince.Text = archive.Province;
                    lblCity.Text = archive.City;
                    //lblCounty.Text = archive.County;
                    lblContractCode.Text = archive.ContractCode;
                    lblFiledDate.Text = archive.FiledDate.ToString("yyyy-MM-dd");
                    lblKeywords.Text = archive.KeyWords;
                    lblDesignedYear.Text = archive.DesignedYear;
                    lblOrderNumber.Text = archive.OrderNumber.ToString();
                    lblFileNumber.Text = archive.QueryNumber;
                    //lblCustomerId.Text = archive.CustomerId;
                    lblFileCategory.Text = archive.Category;
                    if (!archive.IsScaleNull())
                    {
                        lblScale.Text = archive.Scale.ToString();
                    }
                    lblConstructForm.Text = archive.ConstructForm;
                    lblDesignStage.Text = archive.DesignStage;
                    lblBelongedTo.Text = archive.BelongedTo;
                    lblProjectManager.Text = archive.ProjectManager;

                    //lblExecutor.Text = archive.IsExecutorNull() ? "" : archive.Executor;

                    if (!archive.IsBuildingBlueprintNull() && archive.BuildingBlueprint.Length > 0)
                    {
                        //lblBuildingBlueprint.Text = archive.BuildingBlueprint.Substring(archive.BuildingBlueprint.LastIndexOf(System.IO.Path.AltDirectorySeparatorChar) + 1);
                        //lblBuildingBlueprint.NavigateUrl = archive.BuildingBlueprint;
                        lblBuildingBlueprint.Visible = true;

                        SiteUtil.GenernateDownloadHyperLink(lblBuildingBlueprint, archive.BuildingBlueprint);
                    }


                    if (!archive.IsElectricalBlueprintNull() && archive.ElectricalBlueprint.Length > 0)
                    {
                        //lblElectricalBlueprint.Text = archive.ElectricalBlueprint.Substring(archive.ElectricalBlueprint.LastIndexOf(System.IO.Path.AltDirectorySeparatorChar) + 1);
                        //lblElectricalBlueprint.NavigateUrl = archive.ElectricalBlueprint;
                        lblElectricalBlueprint.Visible = true;
                        SiteUtil.GenernateDownloadHyperLink(lblElectricalBlueprint, archive.ElectricalBlueprint);
                    }

                    if (!archive.IsMechanicalMaterialManifestNull() && archive.MechanicalMaterialManifest.Length > 0)
                    {
                        //lblMechanicalMaterialManifest.Text = archive.MechanicalMaterialManifest.Substring(archive.MechanicalMaterialManifest.LastIndexOf(System.IO.Path.AltDirectorySeparatorChar) + 1);
                        //lblMechanicalMaterialManifest.NavigateUrl = archive.MechanicalMaterialManifest;
                        lblMechanicalMaterialManifest.Visible = true;
                        SiteUtil.GenernateDownloadHyperLink(lblMechanicalMaterialManifest, archive.MechanicalMaterialManifest);
                    }

                    if (!archive.IsElectricalMaterialManifestNull() && archive.ElectricalMaterialManifest.Length > 0)
                    {
                        //lblElectricalMaterialManifest.Text = archive.ElectricalMaterialManifest.Substring(archive.ElectricalMaterialManifest.LastIndexOf(System.IO.Path.AltDirectorySeparatorChar) + 1);
                        //lblElectricalMaterialManifest.NavigateUrl = archive.ElectricalMaterialManifest;
                        lblElectricalMaterialManifest.Visible = true;
                        SiteUtil.GenernateDownloadHyperLink(lblElectricalMaterialManifest, archive.ElectricalMaterialManifest);
                    }

                    if (!archive.IsSystemSolutionNull() && archive.SystemSolution.Length > 0)
                    {
                        //lblSystemSolution.Text = archive.SystemSolution.Substring(archive.SystemSolution.LastIndexOf(System.IO.Path.AltDirectorySeparatorChar) + 1);
                        //lblSystemSolution.NavigateUrl = archive.SystemSolution;
                        lblSystemSolution.Visible = true;
                        SiteUtil.GenernateDownloadHyperLink(lblSystemSolution, archive.SystemSolution);
                    }

                    if (!archive.IsElectricalSolutionNull() && archive.ElectricalSolution.Length > 0)
                    {
                        //lblElectricalSolution.Text = archive.ElectricalSolution.Substring(archive.ElectricalSolution.LastIndexOf(System.IO.Path.AltDirectorySeparatorChar) + 1);
                        //lblElectricalSolution.NavigateUrl = archive.ElectricalSolution;
                        lblElectricalSolution.Visible = true;
                        SiteUtil.GenernateDownloadHyperLink(lblElectricalSolution, archive.ElectricalSolution);
                    }

                    if (!archive.IsDesignSketchNull() && archive.DesignSketch.Length > 0)
                    {
                        lblDesignSketch.Visible = true;
                        SiteUtil.GenernateDownloadHyperLink(lblDesignSketch, archive.DesignSketch);
                    }

                    if (!archive.IsTenderDocumentationNull() && archive.TenderDocumentation.Length > 0)
                    {
                        lblTenderDocumentation.Visible = true;
                        SiteUtil.GenernateDownloadHyperLink(lblTenderDocumentation, archive.TenderDocumentation);
                    }

                    if (!archive.IsPreliminaryDesignNull() && archive.PreliminaryDesign.Length > 0)
                    {
                        lblPreliminaryDesign.Visible = true;
                        SiteUtil.GenernateDownloadHyperLink(lblPreliminaryDesign, archive.PreliminaryDesign);
                    }

                    if (!archive.IsOtherDocumentationNull() && archive.OtherDocumentation.Length > 0)
                    {
                        lblOtherDocumentation.Visible = true;
                        SiteUtil.GenernateDownloadHyperLink(lblOtherDocumentation, archive.OtherDocumentation);
                    }


                    #endregion

                    #region

                    if (!String.IsNullOrEmpty(archive.Description))
                    {
                        DataSet ds = new DataSet();
                        System.IO.StringReader sw = new System.IO.StringReader(archive.Description);
                        ds.ReadXml(sw);

                        txtBuildingBlueprint.Value = ReadAuthority(ds, "BuildingBlueprint");
                        txtBuildingBlueprintReadOnly.Value = txtBuildingBlueprint.Value;

                        txtElectricalBlueprint.Value = ReadAuthority(ds, "ElectricalBlueprint");
                        txtElectricalBlueprintReadOnly.Value = txtElectricalBlueprint.Value;

                        txtMechanicalMaterialManifest.Value = ReadAuthority(ds, "MechanicalMaterialManifest");
                        txtMechanicalMaterialManifestReadOnly.Value = txtMechanicalMaterialManifest.Value;

                        txtElectricalMaterialManifest.Value = ReadAuthority(ds, "ElectricalMaterialManifest");
                        txtElectricalMaterialManifestReadOnly.Value = txtElectricalMaterialManifest.Value;

                        txtElectricalSolution.Value = ReadAuthority(ds, "ElectricalSolution");
                        txtElectricalSolutionReadOnly.Value = txtElectricalSolution.Value;

                        txtSystemSolution.Value = ReadAuthority(ds, "SystemSolution");
                        txtSystemSolutionReadOnly.Value = txtSystemSolution.Value;

                        txtDesignSketch.Value = ReadAuthority(ds, "DesignSketch");
                        txtDesignSketchReadOnly.Value = txtDesignSketch.Value;

                        txtTenderDocumentation.Value = ReadAuthority(ds, "TenderDocumentation");
                        txtTenderDocumentationReadOnly.Value = txtTenderDocumentation.Value;

                        txtPreliminaryDesign.Value = ReadAuthority(ds, "PreliminaryDesign");
                        txtPreliminaryDesignReadOnly.Value = txtPreliminaryDesign.Value;

                        txtOtherDocumentation.Value = ReadAuthority(ds, "OtherDocumentation");
                        txtOtherDocumentationReadOnly.Value = txtOtherDocumentation.Value;
                    }

                    #endregion


                    //
                }
            }
        }

        private string ReadAuthority(DataSet ds, string doc)
        {
            string users = "";
            DataTable dt = ds.Tables[0];

            foreach (DataRow dr in dt.Rows)
            {
                if (dr["Doc"].ToString() == doc)
                {
                    users = dr["Users"].ToString();
                    break;
                }
            }

            return users;
        }

        protected void tvEmp_Load(object sender, EventArgs e)
        {
            this.tvEmp.Attributes.Add("OnClick", "OnTreeNodeChecked(event)");
        }

        protected void btnShowPopup_Click(object sender, EventArgs e)
        {
            string[] assignedBy = null;
            LinkButton btn = sender as LinkButton;
            if (btn != null)
            {
                txtSelectedObject.Value = btn.CommandName;

                switch (btn.CommandName)
                {
                    case "BuildingBlueprint":
                        assignedBy = txtBuildingBlueprint.Value.Split(',');
                        break;
                    case "ElectricalBlueprint":
                        assignedBy = txtElectricalBlueprint.Value.Split(',');
                        break;
                    case "MechanicalMaterialManifest":
                        assignedBy = txtMechanicalMaterialManifest.Value.Split(',');
                        break;
                    case "ElectricalMaterialManifest":
                        assignedBy = txtElectricalMaterialManifest.Value.Split(',');
                        break;
                    case "ElectricalSolution":
                        assignedBy = txtElectricalSolution.Value.Split(',');
                        break;
                    case "SystemSolution":
                        assignedBy = txtSystemSolution.Value.Split(',');
                        break;
                    case "DesignSketch":
                        assignedBy = txtDesignSketch.Value.Split(',');
                        break;
                    case "TenderDocumentation":
                        assignedBy = txtTenderDocumentation.Value.Split(',');
                        break;
                    case "PreliminaryDesign":
                        assignedBy = txtPreliminaryDesign.Value.Split(',');
                        break;
                    case "OtherDocumentation":
                        assignedBy = txtOtherDocumentation.Value.Split(',');
                        break;
                    

                    default:
                        break;
                }

                
            }

            tvEmp.Nodes.Clear();
            HumanResources.HumanResourcesHelper.PopulateEmployeeTree(tvEmp, true, assignedBy);//null 

            updPnlDepartment.Update();
            mdlDetailView.Show();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            // 保存弹出窗口所更新的信息
            string employees = "";
            string employeesId = "";

            if (tvEmp.CheckedNodes.Count > 0)
            {
                foreach (TreeNode node in tvEmp.CheckedNodes)
                {
                    if (!node.Text.StartsWith("["))
                    {
                        employees += node.Text + ",";
                        employeesId += node.Value + ",";
                    }
                }
            }

            switch (txtSelectedObject.Value)
            {
                case "BuildingBlueprint":
                    txtBuildingBlueprintReadOnly.Value = employees.Trim(',');
                    txtBuildingBlueprint.Value = employees.Trim(',');
                    break;
                case "ElectricalBlueprint":
                    txtElectricalBlueprintReadOnly.Value = employees.Trim(',');
                    txtElectricalBlueprint.Value = employees.Trim(',');
                    break;
                case "MechanicalMaterialManifest":
                    txtMechanicalMaterialManifestReadOnly.Value = employees.Trim(',');
                    txtMechanicalMaterialManifest.Value = employees.Trim(',');
                    break;
                case "ElectricalMaterialManifest":
                    txtElectricalMaterialManifestReadOnly.Value = employees.Trim(',');
                    txtElectricalMaterialManifest.Value = employees.Trim(',');
                    break;
                case "ElectricalSolution":
                    txtElectricalSolutionReadOnly.Value = employees.Trim(',');
                    txtElectricalSolution.Value = employees.Trim(',');
                    break;
                case "SystemSolution":
                    txtSystemSolutionReadOnly.Value = employees.Trim(',');
                    txtSystemSolution.Value = employees.Trim(',');
                    break;
                case "DesignSketch":
                    txtDesignSketchReadOnly.Value = employees.Trim(',');
                    txtDesignSketch.Value = employees.Trim(',');
                    break;
                case "TenderDocumentation":
                    txtTenderDocumentationReadOnly.Value = employees.Trim(',');
                    txtTenderDocumentation.Value = employees.Trim(',');
                    break;
                case "PreliminaryDesign":

                    txtPreliminaryDesignReadOnly.Value = employees.Trim(',');
                    txtPreliminaryDesign.Value = employees.Trim(',');
                    break;
                case "OtherDocumentation":
                    txtOtherDocumentationReadOnly.Value = employees.Trim(',');
                    txtOtherDocumentation.Value = employees.Trim(',');
                    break;


                default:
                    break;
            }

            
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string id = HttpContext.Current.Request.QueryString["id"];
            if(String.IsNullOrEmpty(id))
                return;
            DataTable dt = FillTable();
            if(dt.Rows.Count > 0)
            {
                DataSet ds = new DataSet();
                ds.Tables.Add(dt);
                string xml = ds.GetXml();

                Archive.BusinessActions.Archive.UpdateAuthority(new Guid(id), xml);

                lblJavaScript.Text = "<script type=\"text/javascript\">alert('保存成功！');</script>";
            }
            
        }

        private DataTable FillTable()
        {
            DataTable dt = GenerateTable();

            DataRow dr = dt.NewRow();
            if(txtBuildingBlueprint.Value.Length > 0)
            {
                dr = dt.NewRow();
                dr["Doc"] = "BuildingBlueprint";
                dr["Users"] = txtBuildingBlueprint.Value;
                dt.Rows.Add(dr);
            }
            if(txtElectricalBlueprint.Value.Length > 0)
            {
                dr = dt.NewRow();
                dr["Doc"] = "ElectricalBlueprint";
                dr["Users"] = txtElectricalBlueprint.Value;
                dt.Rows.Add(dr);
            }
            if(txtMechanicalMaterialManifest.Value.Length > 0)
            {
                dr = dt.NewRow();
                dr["Doc"] = "MechanicalMaterialManifest";
                dr["Users"] = txtMechanicalMaterialManifest.Value;
                dt.Rows.Add(dr);
            }
            if(txtElectricalMaterialManifest.Value.Length > 0)
            {
                dr = dt.NewRow();
                dr["Doc"] = "ElectricalMaterialManifest";
                dr["Users"] = txtElectricalMaterialManifest.Value;
                dt.Rows.Add(dr);
            }
            if(txtElectricalSolution.Value.Length > 0)
            {
                dr = dt.NewRow();
                dr["Doc"] = "ElectricalSolution";
                dr["Users"] = txtElectricalSolution.Value;
                dt.Rows.Add(dr);
            }
            if(txtSystemSolution.Value.Length > 0)
            {
                dr = dt.NewRow();
                dr["Doc"] = "SystemSolution";
                dr["Users"] = txtSystemSolution.Value;
                dt.Rows.Add(dr);
            }

            if(txtDesignSketch.Value.Length > 0)
            {
                dr = dt.NewRow();
                dr["Doc"] = "DesignSketch";
                dr["Users"] = txtDesignSketch.Value;
                dt.Rows.Add(dr);
            }
            if(txtTenderDocumentation.Value.Length > 0)
            {
                dr = dt.NewRow();
                dr["Doc"] = "TenderDocumentation";
                dr["Users"] = txtTenderDocumentation.Value;
                dt.Rows.Add(dr);
            }
            if(txtPreliminaryDesign.Value.Length > 0)
            {
                dr = dt.NewRow();
                dr["Doc"] = "PreliminaryDesign";
                dr["Users"] = txtPreliminaryDesign.Value;
                dt.Rows.Add(dr);
            }

            if(txtOtherDocumentation.Value.Length > 0)
            {
                dr = dt.NewRow();
                dr["Doc"] = "OtherDocumentation";
                dr["Users"] = txtOtherDocumentation.Value;
                dt.Rows.Add(dr);
            }

            return dt;

        }

        private DataTable GenerateTable()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("Doc", typeof(String));
            //dt.Columns.Add("Doc", typeof(String));
            dt.Columns.Add("Users", typeof(String));

            return dt;
        }
    }
}
